package com.jxchexie.admin;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.jxchexie.signin.R;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

import static com.jxchexie.api.Api.SEND_MESSAGE;

public class SendMessageActivity extends AppCompatActivity {
        private Button send_message;
        private EditText title;
        private EditText content;
        private String t;
        private String c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_message);
        initView();
        initEvent();
    }

    private void initEvent() {
        send_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t = title.getText().toString();
                c = content.getText().toString();
                if(t.equals("") || c.equals("")){
                    Toast.makeText(SendMessageActivity.this,"标题和内容不能为空！",Toast.LENGTH_SHORT).show();
                }else{
                    /*构造请求体*/
                    HashMap<String,String> params = new HashMap<>();
                    params.put("title", t);
                    params.put("content", c);
                    JSONObject jsonObject = new JSONObject(params);
                    OkGo.post(SEND_MESSAGE)
                            .upJson(jsonObject.toString())
                            .execute(new StringCallback() {
                                @Override
                                public void onSuccess(String s, Call call, Response response) {
                                    Toast.makeText(SendMessageActivity.this,"推送成功",Toast.LENGTH_SHORT).show();
                                }
                            });

                }
            }
        });
    }

    private void initView() {
        send_message = (Button) findViewById(R.id.send_message);
        title = (EditText) findViewById(R.id.title);
        content = (EditText) findViewById(R.id.content);
    }

}